import { _decorator, Component, view, ResolutionPolicy, screen } from 'cc';
import Common from './Config/Common';
import { DataConfig } from './Config/DataConfig';
import { GameEventType } from './Event/EnumDefine';
import { EventManager } from './Event/EventManager';
const { ccclass } = _decorator;

@ccclass('ScreenAdapter')
export class ScreenAdapter extends Component {

    onLoad() {
        this.onScreen();
        screen.on('window-resize', this.onScreen.bind(this));
    }

    onScreen() {
        
        let size = DataConfig.size;
        const visibleSize = view.getVisibleSize();
        const ratioVisible = visibleSize.width / visibleSize.height;
        const ratioDesign = size.width/size.height;
        if (ratioVisible > ratioDesign) {
            view.setDesignResolutionSize(size.width, size.height, ResolutionPolicy.FIXED_HEIGHT);
        }
        else{
            view.setDesignResolutionSize(size.width, size.height, ResolutionPolicy.FIXED_WIDTH);
        }
        EventManager.instance.triggerEvent(GameEventType.Screen_Change, null);
        console.log(`----------visibleSize: ${visibleSize} screenSize: ${Common.instance.getScreenSize()}`);
    }

    // private updateAdapter(): void {
    //     let winSize = new Size(screen.windowSize.width / view['_scaleX'], screen.windowSize.height / view['_scaleY']);
    //     let ut = this.node.getComponent(UITransform);
    //     if (AUTO_ROTATE) {
    //         let designSize = view.getDesignResolutionSize();
    //         if (ut.width < ut.height === designSize.width < designSize.height) {
    //             this.cameraNode.angle !== 0 && tween(this.cameraNode).to(ROTATE_TIME, { angle: 0 }, { easing: 'expoOut' }).start();
    //         } else {
    //             [winSize.width, winSize.height] = [winSize.height, winSize.width];
    //             this.cameraNode.angle !== -90 && tween(this.cameraNode).to(ROTATE_TIME, { angle: -90 }, { easing: 'expoOut' }).start();
    //         }
    //     }
    //     switch (this.adapterMode) {
    //         case AdapterMode.拉伸全屏:
    //             this.rootNode.setScale(winSize.width / ut.width, winSize.height / ut.height);
    //             this.node.setPosition(winSize.width * (ut.anchorX - 0.5), winSize.height * (ut.anchorY - 0.5));
    //             break;
    //         case AdapterMode.等比缩放:
    //             let scale = winSize.width / winSize.height > ut.width / ut.height ? winSize.height / ut.height : winSize.width / ut.width;
    //             this.rootNode.setScale(scale, scale);
    //             this.node.setPosition(ut.width * (ut.anchorX - 0.5), ut.height * (ut.anchorY - 0.5));
    //             break;
    //     }
    // }
}